package com.yiwei.jmx.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.activity.WechatChatActivity;
import com.yiwei.jmx.adapter.ChannelTypeAdapter;
import com.yiwei.jmx.adapter.HomeOneAdapter;
import com.yiwei.jmx.adapter.HomeTwoAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.model.DealIndexModel;
import com.yiwei.jmx.event.UserEvent;
import com.yiwei.jmx.event.UserUnLoginEvent;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.V19RelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-8-29.
 */

public class MainHomeFragment extends BaseFragment implements NestedScrollView.OnScrollChangeListener {


    @BindView(R.id.list_channel_type)
    RecyclerView listChannelType;
    @BindView(R.id.list_one)
    RecyclerView listOne;
    @BindView(R.id.list_two)
    RecyclerView listTwo;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.btn_notice)
    ImageView btnNotice;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_toolbar)
    V19RelativeLayout layoutToolbar;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private HomeOneAdapter homeOneAdapter;
    private HomeTwoAdapter homeTwoAdapter;
    private float headerHeight;
    private float minHeaderHeight;
    private Unbinder unbinder;
    private ChannelTypeAdapter channelTypeAdapter;


    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main_home;
    }

    @Override
    protected void initView() {
        headerHeight = getResources().getDimensionPixelSize(R.dimen.y200);
        minHeaderHeight = getResources().getDimension(R.dimen.y98);
        scrollView.setOnScrollChangeListener(this);
        homeOneAdapter = new HomeOneAdapter(getContext());
        listOne.setLayoutManager(new GridLayoutManager(getContext(), 2));
        listOne.setAdapter(homeOneAdapter);

        homeTwoAdapter = new HomeTwoAdapter(getContext());
        listTwo.setLayoutManager(new GridLayoutManager(getContext(), 3));
        listTwo.setAdapter(homeTwoAdapter);


        channelTypeAdapter = new ChannelTypeAdapter(getContext());
        listChannelType.setAdapter(channelTypeAdapter);


        listChannelType.setNestedScrollingEnabled(false);

        listOne.setNestedScrollingEnabled(false);
        listTwo.setNestedScrollingEnabled(false);


        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDealIndex();
            }
        });
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UserEvent item) {
        getDealIndex();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UserUnLoginEvent item) {
        getDealIndex();
    }

    @Override
    public void onResume() {
        super.onResume();
        getDealIndex();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    public void getDealIndex() {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getDealIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setDealIndexData(result.data);
                });
    }

    private void setDealIndexData(DealIndexModel.DataBean data) {
//        homeOneAdapter.update(data.life);
        homeTwoAdapter.update(data.rookie);

        channelTypeAdapter.update(data.channel);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }


    @Override
    public void onScrollChange(NestedScrollView v, int x, int y, int oldScrollX, int oldScrollY) {
        if (refresh != null) {
            refresh.setEnabled(scrollView.getScrollY() == 0);
        }

        //Y轴偏移量
        float scrollY = scrollView.getScrollY();
//        e3483d
        //变化率
        float headerBarOffsetY = headerHeight - minHeaderHeight;//Toolbar与header高度的差值
        float offset = 1 - Math.max((headerBarOffsetY - scrollY) / headerBarOffsetY, 0f);
        if (scrollY >= headerHeight) {
            layoutToolbar.setBackgroundColor(Color.argb(255, 0, 114, 255));
        } else {

            if (scrollY == 0) {

            } else {
                layoutToolbar.setBackgroundColor(Color.argb((int) (offset * 255), 0, 114, 255));
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(refresh!=null){
            refresh.setRefreshing(false);
        }
    }
    @OnClick({R.id.btn_notice, R.id.btn_wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_notice:

                break;
            case R.id.btn_wechat:
                Util.startActivity(getContext(), WechatChatActivity.class);
                break;
        }
    }
}
